using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Pitoshka.Web.UrlRewriter
{
    public class RewriteHelper
    {
        private string applicationPath;
        public RewriteHelper()
        {
            this.applicationPath = (HttpRuntime.AppDomainAppVirtualPath.Length > 1) ? HttpRuntime.AppDomainAppVirtualPath : String.Empty;

        }
        public string HandleRootOperator(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                if (url.StartsWith("^~/"))
                    return "^" + this.applicationPath + url.Substring(2);
                else if (url.StartsWith("~/"))
                    return this.applicationPath + url.Substring(1);
            }
            return url;
        }
        
        public string JoinUrlParameter(string destinationUrl, string requestQuerystring)
        {
            int pos = destinationUrl.IndexOf('?');
            if (requestQuerystring.Length > 0)
            {
                if (pos >= 0)
                {
                    destinationUrl += "&" + requestQuerystring;
                }
                else
                {
                    destinationUrl += "?" + requestQuerystring;
                }
            }
            return destinationUrl;
        }
    }
}
